package com.java110.mall.acct.payment.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreOrderReturnPayDto;
import com.java110.intf.cart.IUnifiedOrderInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentBusiness;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("buyServ")
public class BuyServBusiness implements IPaymentBusiness {
    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IUnifiedOrderInnerServiceSMO unifiedOrderInnerServiceSMOImpl;
    @Override
    public PaymentOrderDto unified(ICmdDataFlowContext context, JSONObject reqJson) {
        String appId = context.getReqHeaders().get("app-id");
        String userId = context.getReqHeaders().get("user-id");
        reqJson.put("user-id", userId);//
        reqJson.put("appId", appId);//
        //todo 查询商铺信息
        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");
        shopDto = shopDtos.get(0);

        StoreOrderReturnPayDto storeOrderReturnPayDto = unifiedOrderInnerServiceSMOImpl.unifiedOrder(reqJson);
        String orderId = storeOrderReturnPayDto.getOrderId();
        String feeName = "家政服务";
        double money = storeOrderReturnPayDto.getPayPrice();

        PaymentOrderDto paymentOrderDto = new PaymentOrderDto();
        paymentOrderDto.setOrderId(orderId);
        paymentOrderDto.setMoney(money);
        paymentOrderDto.setName(feeName);
        if (StringUtil.isNumber(shopDto.getServiceFee()) && Double.parseDouble(shopDto.getServiceFee()) > 0) {
            paymentOrderDto.setProfitSharing(true);
        }

        return paymentOrderDto;
    }

    @Override
    public void notifyPayment(PaymentOrderDto paymentOrderDto, JSONObject reqJson) {
        reqJson.put("orderId", paymentOrderDto.getOrderId());
        reqJson.put("payOrderId", paymentOrderDto.getTransactionId());
        ResponseEntity<String> responseEntity = unifiedOrderInnerServiceSMOImpl.payNotifyOrder(reqJson);
    }
}
